import Link from "next/link";
import axios from "axios";

const Tasks = ({ tasks }) => {
  return (
    <div style={{ padding: "20px" }}>
      <Link href="/">
        <a>返回首页</a>
      </Link>
      <h2>tasks</h2>
      <ul>
        {
          tasks.map((item, index) => <li key={index}>{item}</li>)
        }
      </ul>
    </div>
  );
};
// 请求数据 getInitialProps
Tasks.getInitialProps = async () => {
  const res = await new Promise((resolve) => {
    axios.get("https://www.fastmock.site/mock/21a510471fabc5e57f4ef5267cb2dbf5/reactRedux/taskList").then((response) => {
      resolve(response)
    }).catch((error) => console.log(error));
  });
  // 必须返回一个对象
  return { tasks: res.data }
};

export default Tasks;
